//
//  ClubIntroDetailViewController.swift
//  77Club
//
//  Created by Apple on 2024/11/7.
//

import UIKit

class ClubIntroDetailViewController: ClubBaseViewController {

    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let scrollView = UIScrollView()
    private let containerView = UIView()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#323233").font(14.font_semiBold)
        .text("游戏简介").end()
    private let contentLabel = c_make_ex(UILabel.self)
        .color("#323233").font(14.font_regular).line(0).end()
    private let changjiaLabel = c_make_ex(UILabel.self)
        .color("#323233").font(14.font_semiBold)
        .text("游戏厂商").end()
    private let changValueLabel = c_make_ex(UILabel.self)
        .color("#323233").font(14.font_regular).line(0).end()
    private let timeLabel = c_make_ex(UILabel.self)
        .color("#323233").font(14.font_semiBold)
        .text("上架时间").end()
    private let timeValueLabel = c_make_ex(UILabel.self)
        .color("#323233").font(14.font_regular).line(0).end()
    private let configLabel = c_make_ex(UILabel.self)
        .color("#323233").font(14.font_semiBold)
        .text("配置要求").end()
    private let configValueLabel = c_make_ex(UILabel.self)
        .color("#323233").font(14.font_regular).line(0).end()
    private let tagLabel = c_make_ex(UILabel.self)
        .color("#323233").font(14.font_semiBold)
        .text("游戏标签").end()
    private let tagValueLabel = c_make_ex(UILabel.self)
        .color("#323233").font(14.font_regular).line(0).end()
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // 禁用侧滑手势的代理
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        // 恢复默认代理
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }
    
    var infoModel: ClubGameDetailModel? {
        didSet{
            guard let md = infoModel else { return }
            let tagStr = md.biaoqian.joined(separator: " · ")
            tagValueLabel.text = tagStr
        }
    }
    var model: ClubGameDetailInfoModel? {
        didSet{
            guard let md = model else { return }
            if let data = md.content.data(using: .utf8) {
                // 将 HTML 转换为 NSAttributedString
                let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
                    .documentType: NSAttributedString.DocumentType.html,
                    .characterEncoding: String.Encoding.utf8.rawValue
                ]
                if let attributedString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) {
                    // 将 NSAttributedString 转换为 NSMutableAttributedString
                    let mutableAttributedString = NSMutableAttributedString(attributedString: attributedString)
                    //
                    let fullRange = NSRange(location: 0, length: mutableAttributedString.length)
                    mutableAttributedString.addAttribute(.foregroundColor, value: "#323233".toColor, range: fullRange)
                    mutableAttributedString.addAttribute(.font, value: 14.font_medium!, range: fullRange)
                    contentLabel.attributedText = mutableAttributedString // 设置富文本
                }
            }
            
            if md.changshang.count > 0 {
                changjiaLabel.isHidden = false
                changValueLabel.isHidden = false
                changValueLabel.text = "开发商：\(md.changshang)"
            }else{
                changjiaLabel.isHidden = true
                changValueLabel.isHidden = true
            }
            if md.shangjia.count > 0 {
                timeLabel.isHidden = false
                timeValueLabel.isHidden = false
                timeValueLabel.text = md.shangjia
            }else{
                timeLabel.isHidden = true
                timeValueLabel.isHidden = true
            }
            if md.pzyq.count > 0 {
                configLabel.isHidden = false
                configValueLabel.isHidden = false
                let configStr = md.pzyq.replacingOccurrences(of: "\r", with: "\n")
                configValueLabel.text = configStr
            }else{
                configLabel.isHidden = true
                configValueLabel.isHidden = true
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            scrollView.subviews{
                containerView.subviews{
                    titleLabel
                    contentLabel
                    changjiaLabel
                    changValueLabel
                    timeLabel
                    timeValueLabel
                    configLabel
                    configValueLabel
                    tagLabel
                    tagValueLabel
                }
            }
        }
        
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        scrollView.top(CGFloat.navi_height).leading(0).width(.screenWidth).bottom(0)
        containerView.fillContainer().width(.screenWidth)
        titleLabel.leading(12.0.Scale).top(11.0.Scale)
        contentLabel.leading(12.0.Scale).trailing(12.0.Scale)
        contentLabel.Top == titleLabel.Bottom + 11.0.Scale
        changjiaLabel.leading(12.0.Scale)
        changjiaLabel.Top == contentLabel.Bottom + 23.0.Scale
        changValueLabel.leading(12.0.Scale).trailing(12.0.Scale)
        changValueLabel.Top == changjiaLabel.Bottom + 15.0.Scale
        timeLabel.leading(12.0.Scale)
        timeLabel.Top == changValueLabel.Bottom + 27.0.Scale
        timeValueLabel.leading(12.0.Scale).trailing(12.0.Scale)
        timeValueLabel.Top == timeLabel.Bottom + 15.0.Scale
        configLabel.leading(12.0.Scale)
        configLabel.Top == timeValueLabel.Bottom + 27.0.Scale
        configValueLabel.leading(12.0.Scale).trailing(12.0.Scale)
        configValueLabel.Top == configLabel.Bottom + 15.0.Scale
        tagLabel.leading(12.0.Scale)
        tagLabel.Top == configValueLabel.Bottom + 27.0.Scale
        tagValueLabel.leading(12.0.Scale).trailing(12.0.Scale).bottom(>=.safe_bottom)
        tagValueLabel.Top == tagLabel.Bottom + 15.0.Scale
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }
}
